package cn.iocoder.yudao.module.shop.service.statistics;


import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.iocoder.yudao.framework.common.enums.UserTypeEnum;
import cn.iocoder.yudao.framework.common.util.collection.CollectionUtils;
import cn.iocoder.yudao.module.member.api.user.MemberUserApi;
import cn.iocoder.yudao.module.member.api.user.dto.MemberUserRespDTO;
import cn.iocoder.yudao.module.member.dal.dataobject.user.MemberUserDO;
import cn.iocoder.yudao.module.member.dal.mysql.user.MemberUserMapper;
import cn.iocoder.yudao.module.pay.dal.dataobject.wallet.PayWalletDO;
import cn.iocoder.yudao.module.pay.dal.dataobject.wallet.PayWalletRechargeDO;
import cn.iocoder.yudao.module.pay.dal.mysql.wallet.PayWalletMapper;
import cn.iocoder.yudao.module.pay.dal.mysql.wallet.PayWalletRechargeMapper;
import cn.iocoder.yudao.module.pay.service.wallet.PayWalletRechargeService;
import cn.iocoder.yudao.module.pay.service.wallet.PayWalletService;
import cn.iocoder.yudao.module.shop.controller.admin.handlestrategyuser.vo.ShopHandleStrategyUserRespVO;
import cn.iocoder.yudao.module.shop.controller.admin.member.vo.MemberUserStatisticsReqVO;
import cn.iocoder.yudao.module.shop.controller.admin.statistics.vo.*;
import cn.iocoder.yudao.module.shop.dal.dataobject.accountrecord.ShopAccountRecordDO;
import cn.iocoder.yudao.module.shop.dal.dataobject.daystatistics.ShopDayStatisticsDO;
import cn.iocoder.yudao.module.shop.dal.dataobject.dayuser.ShopDayUserDO;
import cn.iocoder.yudao.module.shop.dal.dataobject.handlestrategy.ShopHandleStrategyDO;
import cn.iocoder.yudao.module.shop.dal.dataobject.monthmember.ShopMonthMemberDO;
import cn.iocoder.yudao.module.shop.dal.dataobject.opthistory.ShopOptHistoryDO;
import cn.iocoder.yudao.module.shop.dal.dataobject.order.ShopOrderDO;
import cn.iocoder.yudao.module.shop.dal.dataobject.order.ShopOrderItemDO;
import cn.iocoder.yudao.module.shop.dal.dataobject.statistics.ShopMemberStatisticsDO;
import cn.iocoder.yudao.module.shop.dal.dataobject.statistics.ShopUserStatisticsDO;
import cn.iocoder.yudao.module.shop.dal.dataobject.statistics.ShopWalletStatisticsDO;

import cn.iocoder.yudao.module.shop.dal.dataobject.storerecord.ShopStoreRecordDO;
import cn.iocoder.yudao.module.shop.dal.dataobject.userholiday.ShopUserHolidayDO;
import cn.iocoder.yudao.module.shop.dal.mysql.item.ShopItemMapper;
import cn.iocoder.yudao.module.shop.dal.mysql.opthistory.ShopOptHistoryMapper;
import cn.iocoder.yudao.module.shop.dal.mysql.order.ShopOrderItemMapper;
import cn.iocoder.yudao.module.shop.dal.mysql.order.ShopOrderMapper;
import cn.iocoder.yudao.module.shop.enums.store.ChargeTypeEnum;
import cn.iocoder.yudao.module.shop.service.accountrecord.ShopAccountRecordService;
import cn.iocoder.yudao.module.shop.service.daystatistics.ShopDayStatisticsService;
import cn.iocoder.yudao.module.shop.service.dayuser.ShopDayUserService;
import cn.iocoder.yudao.module.shop.service.handlestrategy.ShopHandleStrategyService;
import cn.iocoder.yudao.module.shop.service.handlestrategyuser.ShopHandleStrategyUserService;
import cn.iocoder.yudao.module.shop.service.itemtimesrecharge.ShopItemTimesRechargeService;
import cn.iocoder.yudao.module.shop.service.masterstatistics.ShopMasterStatisticsService;
import cn.iocoder.yudao.module.shop.service.monthmember.ShopMonthMemberService;
import cn.iocoder.yudao.module.shop.service.storerecord.ShopStoreRecordService;
import cn.iocoder.yudao.module.shop.service.userholiday.ShopUserHolidayService;
import cn.iocoder.yudao.module.system.api.user.AdminUserApi;
import cn.iocoder.yudao.module.system.api.user.dto.AdminUserRespDTO;
import cn.iocoder.yudao.module.system.dal.dataobject.tenantdetail.TenantDetailDO;
import cn.iocoder.yudao.module.system.service.tenantdetail.TenantDetailService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;
import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.shop.enums.ErrorCodeConstants.*;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.*;

@Service
@Validated
public class ShopStatisticsServiceImpl implements ShopStatisticsService{

    @Resource
    private PayWalletMapper payWalletMapper;

    @Resource
    private MemberUserMapper memberUserMapper;

    @Resource
    private ShopOrderMapper shopOrderMapper;

    @Resource
    private ShopOrderItemMapper shopOrderItemMapper;

    @Resource
    private ShopItemMapper shopItemMapper;

    @Resource
    private PayWalletRechargeMapper payWalletRechargeMapper;

    @Resource
    private AdminUserApi adminUserApi;

    @Resource
    private ShopOptHistoryMapper shopOptHistoryMapper;

    @Resource
    private ShopDayStatisticsService shopDayStatisticsService;

    @Resource
    private ShopDayUserService shopDayUserService;

    @Resource
    private ShopMonthMemberService shopMonthMemberService;

    @Resource
    private ShopHandleStrategyService shopHandleStrategyService;

    @Resource
    private ShopHandleStrategyUserService shopHandleStrategyUserService;

    @Resource
    private ShopUserHolidayService shopUserHolidayService;

    @Resource
    private PayWalletService payWalletService;

    @Resource
    private PayWalletRechargeService payWalletRechargeService;

    @Resource
    private ShopAccountRecordService shopAccountRecordService;

    @Resource
    private ShopStoreRecordService shopStoreRecordService;

    @Resource
    private ShopItemTimesRechargeService shopItemTimesRechargeService;

    @Resource
    private ShopMasterStatisticsService shopMasterStatisticsService;

    @Resource
    private MemberUserApi memberUserApi;

    @Resource
    private TenantDetailService tenantDetailService;

    @Override
    public ShopMemberStatisticsDO getMemberStatistics(MemberUserStatisticsReqVO reqVO){
        QueryWrapper<MemberUserDO> qw = null;

        if(reqVO.getTenantId() != null){
            qw = new QueryWrapper<MemberUserDO>()
                    .select("COUNT(*) as total," +
                            " SUM(CASE WHEN sex = 1 THEN 1 ELSE 0 END) as maleCount," +
                            " SUM(CASE WHEN sex = 2 THEN 1 ELSE 0 END) as femaleCount")
                    .eq("tenant_id", reqVO.getTenantId());
        }else {
            qw = new QueryWrapper<MemberUserDO>()
                    .select("COUNT(*) as total," +
                            " SUM(CASE WHEN sex = 1 THEN 1 ELSE 0 END) as maleCount," +
                            " SUM(CASE WHEN sex = 2 THEN 1 ELSE 0 END) as femaleCount");
        }

        List<Map<String, Object>> result = memberUserMapper.selectMaps(qw);

        if(result.size() == 0){
            //todo
        }

        ShopMemberStatisticsDO statisticsDO = new ShopMemberStatisticsDO();
        statisticsDO.setTotalCount(Integer.parseInt(result.get(0).getOrDefault("total", 0).toString()));
        statisticsDO.setMaleCount(Integer.parseInt(result.get(0).getOrDefault("maleCount", 0).toString()));
        statisticsDO.setFemaleCount(Integer.parseInt(result.get(0).getOrDefault("femaleCount", 0).toString()));

        return statisticsDO;
    }

    @Override
    public ShopWalletStatisticsDO getWalletStatistics(MemberUserStatisticsReqVO reqVO){
        QueryWrapper<PayWalletDO> qw = null;

        if(reqVO.getTenantId() == null){
            qw = new QueryWrapper<PayWalletDO>().select("SUM(balance) as leftBalance, COUNT(*) as total");
        }else{
            qw = new QueryWrapper<PayWalletDO>().select("SUM(balance) as leftBalance, COUNT(*) as total")
                    .eq("tenant_id", reqVO.getTenantId());
        }

        List<Map<String, Object>> result = payWalletMapper.selectMaps(qw);

        if(result.size() == 0){
            //todo
        }

        ShopWalletStatisticsDO statisticsDO = new ShopWalletStatisticsDO();
        statisticsDO.setTotal(Integer.parseInt(result.get(0).getOrDefault("total", 0).toString()));
        statisticsDO.setLeftBalance(Integer.parseInt(result.get(0).getOrDefault("leftBalance", 0).toString()));
        return statisticsDO;
    }

    @Override
    public ShopUserStatisticsDO getUserStatistics(Long userId){

        String sql = String.format("SUM(CASE WHEN user_id = %s THEN 1 ELSE 0 END) AS orderCount, "+
                "SUM(CASE WHEN user_id = %s AND history_id > 0 THEN 1 ELSE 0 END) AS historyRecordCount", userId, userId);

        QueryWrapper<ShopOrderDO> qw = new QueryWrapper<ShopOrderDO>().select(sql);

        List<Map<String, Object>> result = shopOrderMapper.selectMaps(qw);
        if(result.size() == 0 || result.get(0) == null){
            return new ShopUserStatisticsDO().setUserId(userId).setOrderCount(0).setHistoryRecordCount(0).setDealCount(0);
        }

        ShopUserStatisticsDO statisticsDO = new ShopUserStatisticsDO();
        statisticsDO.setUserId(userId);
        statisticsDO.setOrderCount(Integer.parseInt(result.get(0).getOrDefault("orderCount", 0).toString()));
        statisticsDO.setHistoryRecordCount(Integer.parseInt(result.get(0).getOrDefault("historyRecordCount", 0).toString()));
        statisticsDO.setDealCount(0);

        return statisticsDO;
    }

    @Override
    public ShopJiuliangStatisticsRespVO getJiuliangStatistics(Long tenantId, LocalDateTime startTime, LocalDateTime endTime){
        List<ShopOrderDO> orderDOList = shopOrderMapper.selectList(new LambdaQueryWrapper<ShopOrderDO>()
                .between(ShopOrderDO::getStartTime, startTime, endTime)
                .eq(ShopOrderDO::getPayStatus, 1)
                .eq(ShopOrderDO::getTenantId, tenantId));

        Set<Long> userIds = CollectionUtils.convertSet(orderDOList, ShopOrderDO::getUserId);
        Integer totalCount = 0;
        Integer totalPrice = 0;
        Integer totalMember = orderDOList.size();
        List<Long> orderIds = new ArrayList<>();

        Map<Long, AdminUserRespDTO> userMap = adminUserApi.getUserMap(userIds);
        Map<Long, AdminUserRespDTO> orderUserMap = new HashMap<>();
        Map<Long, Integer> userCountMap = new HashMap<>();

        for(int i = 0; i<orderDOList.size(); i++){
            ShopOrderDO item = orderDOList.get(i);
            totalPrice += item.getPayPrice();
            orderIds.add(item.getId());

            userIds.add(item.getUserId());
            orderUserMap.put(item.getId(), userMap.get(item.getUserId()));
        }

        List<ShopOrderItemDO> orderItemDOList = shopOrderItemMapper.selectList(ShopOrderItemDO::getOrderId, orderIds);
        for(int i = 0; i<orderItemDOList.size(); i++){
            totalCount += orderItemDOList.get(i).getCount();
            Long orderId = orderItemDOList.get(i).getOrderId();
            AdminUserRespDTO user = orderUserMap.get(orderId);
            if(!userCountMap.containsKey(user.getId())){
                userCountMap.put(user.getId(), 0);
            }

            userCountMap.replace(user.getId(), userCountMap.get(user.getId()) + orderItemDOList.get(i).getCount());
        }

        ShopJiuliangStatisticsRespVO respvo = new ShopJiuliangStatisticsRespVO();
        respvo.setTotalCount(totalCount);
        respvo.setTotalMember(totalMember);
        respvo.setTotalPrice(totalPrice);

        List<AdminUserRespDTO> userList = new ArrayList<>();
        List<Long> countList = new ArrayList<>();
        for(Long userId: userCountMap.keySet()){
            AdminUserRespDTO user = userMap.get(userId);
            Integer count = userCountMap.get(userId);

            userList.add(user);
            countList.add(Long.valueOf(count));
        }
        respvo.setUserList(userList);
        respvo.setCountList(countList);

        return respvo;
    }

    @Override
    public ShopRechargeStatisticsRespVO getRechargeStatistics(Long tenantId, LocalDateTime startTime, LocalDateTime endTime){
        List<PayWalletRechargeDO> list = payWalletRechargeMapper.selectList(new LambdaQueryWrapper<PayWalletRechargeDO>()
                .between(PayWalletRechargeDO::getPayTime, startTime, endTime)
                .eq(PayWalletRechargeDO::getTenantId, tenantId));

        Integer totalPrice = 0;
        Integer totalBonus = 0;
        for(int i = 0; i<list.size(); i++){
            totalPrice += list.get(i).getPayPrice();
            totalBonus += list.get(i).getBonusPrice();
        }

        ShopRechargeStatisticsRespVO respVO = new ShopRechargeStatisticsRespVO();
        respVO.setTotalCount(list.size());
        respVO.setTotalPrice(totalPrice);
        respVO.setTotalBonus(totalBonus);

        return respVO;
    }

    @Override
    public ShopJiucaseStatisticsRespVO getJiucaseStatistics(LocalDateTime startTime, LocalDateTime endTime){
        List<ShopOptHistoryDO> historyList = shopOptHistoryMapper.selectList(new LambdaQueryWrapper<ShopOptHistoryDO>()
                .between(ShopOptHistoryDO::getCreateTime, startTime, endTime));

        Integer totalCount = historyList.size();
        Set<Long> userIds = CollectionUtils.convertSet(historyList, ShopOptHistoryDO::getUserId);

        Map<Long, AdminUserRespDTO> userMap = adminUserApi.getUserMap(userIds);
        Map<Long, Integer> countMap = new HashMap<>();

        for(ShopOptHistoryDO historyItem : historyList){
            if(!countMap.containsKey(historyItem.getUserId())){
                countMap.put(historyItem.getUserId(), 0);
            }

            countMap.replace(historyItem.getUserId(), countMap.get(historyItem.getUserId()) + 1);
        }

        List<AdminUserRespDTO> userList = new ArrayList<>();
        List<Integer> countList = new ArrayList<>();

        for(Long userId : countMap.keySet()){
            userList.add(userMap.get(userId));
            countList.add(countMap.get(userId));
        }

        ShopJiucaseStatisticsRespVO respVO = new ShopJiucaseStatisticsRespVO();
        respVO.setTotalCount(totalCount);
        respVO.setUserList(userList);
        respVO.setCountList(countList);

        return respVO;
    }


    @Override
    public Boolean recordOrder(ShopOrderDO order){
        Boolean isNewMember = false;

        MemberUserRespDTO member = memberUserApi.getUser(order.getMemberId());
        if(member == null){
            throw exception(ORDER_FIND_MEMBER_ERROR);
        }

        Long count = shopOrderMapper.selectCount(ShopOrderDO::getMemberId, order.getMemberId());
        if(count == 1 && member.getCreateTime().isAfter(LocalDateTime.of(2024,10,1,0,0))){
            isNewMember = true;
        }

        Long rechargeOrderCount = 0L;
        PayWalletDO walletDO = payWalletService.getWallet(order.getMemberId(), UserTypeEnum.MEMBER.getValue());
        if(walletDO != null){
            rechargeOrderCount = payWalletRechargeService.getOrderCountByWalletId(walletDO.getId());
        }

        shopDayStatisticsService.recordOrder(order, isNewMember, rechargeOrderCount);
        shopDayUserService.recordOrder(order, isNewMember, rechargeOrderCount);
        shopMonthMemberService.recordOrder(order, isNewMember, rechargeOrderCount);
        shopMasterStatisticsService.recordOrder(order);
        return true;
    }

    @Override
    public Boolean rollbackOrder(ShopOrderDO order){
        shopDayStatisticsService.rollbackOrder(order);
        shopDayUserService.rollbackOrder(order);
        shopMonthMemberService.rollbackOrder(order);
        shopMasterStatisticsService.rollbackOrder(order);

        return true;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean recordHoliday(LocalDateTime time, Long masterId){
        AdminUserRespDTO user = adminUserApi.getUser(masterId);
        if(user == null){
            throw exception(USER_HOLIDAY_USER_NOT_EXISTS);
        }

        shopDayUserService.recordHoliday(time, user);
        return true;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean recordTimesChargePay(Integer payPrice, Long memberId, Long masterId, Boolean isFirst, Long tenantId){

        Long count = shopOrderMapper.selectCount(ShopOrderDO::getMemberId, memberId);

        ChargeTypeEnum chargeType = ChargeTypeEnum.FIRST;
        if(isFirst == true){
            if(count > 0){
                throw exception(DAY_STATISTICS_FIRST_CHARGE_ERROR_ORDER);
            }

            Long rechargeOrderCount = 0L;
            PayWalletDO walletDO = payWalletService.getWallet(memberId, UserTypeEnum.MEMBER.getValue());
            if(walletDO != null){
                rechargeOrderCount = payWalletRechargeService.getOrderCountByWalletId(walletDO.getId());
            }

            Long timesChargeCount = shopItemTimesRechargeService.getChargeCount(memberId);

            //生成充值记录之后，记录的 所以>1
            if(rechargeOrderCount > 0 || timesChargeCount > 1){
                throw exception(DAY_STATISTICS_FIRST_CHARGE_ERROR_CHARGE);
            }

            chargeType = ChargeTypeEnum.FIRST;
        }else {
            if(count > 0){
                chargeType = ChargeTypeEnum.CONVERT;
            }else {
                chargeType = ChargeTypeEnum.CONTINUE;
            }
        }

        shopDayStatisticsService.recordTimesChargePay(payPrice, memberId, masterId, chargeType, tenantId);
        shopDayUserService.recordTimesChargePay(payPrice, memberId, masterId, chargeType, tenantId);
        shopMonthMemberService.recordTimesChargePay(payPrice, memberId, masterId, chargeType, tenantId);
        if(isFirst){
            shopMasterStatisticsService.recordFirstCharge(masterId, 1, payPrice);
        }

        return true;
    }

    @Override
    public Boolean recordPayOrder(PayWalletRechargeDO payOrder, Long memberId, Long masterId, Boolean isFirst){
        Long count = shopOrderMapper.selectCount(ShopOrderDO::getMemberId, memberId);

        ChargeTypeEnum chargeType = ChargeTypeEnum.FIRST;
        if(isFirst == true){
            if(count > 0){
                throw exception(DAY_STATISTICS_FIRST_CHARGE_ERROR_ORDER);
            }

            Long rechargeOrderCount = rechargeOrderCount = payWalletRechargeService.getOrderCountByWalletId(payOrder.getWalletId());
            Long timesChargeCount = shopItemTimesRechargeService.getChargeCount(memberId);

            //先生成 充值订单，再记录，所以>1
            if(rechargeOrderCount > 1 || timesChargeCount > 0){
                throw exception(DAY_STATISTICS_FIRST_CHARGE_ERROR_CHARGE);
            }

            chargeType = ChargeTypeEnum.FIRST;
        }else {
            if(count > 0){
                chargeType = ChargeTypeEnum.CONVERT;
            }else {
                chargeType = ChargeTypeEnum.CONTINUE;
            }
        }

        shopDayStatisticsService.recordPayOrder(payOrder, masterId, chargeType);
        shopDayUserService.recordPayOrder(payOrder, masterId, chargeType);
        shopMonthMemberService.recordPayOrder(payOrder, masterId, chargeType);
        if(isFirst){
            shopMasterStatisticsService.recordFirstCharge(masterId, 1, payOrder.getPayPrice());
        }
        return true;
    }

    @Override
    public ShopSalaryStatisticsRespVO getSalaryDetail(LocalDateTime startTime, LocalDateTime endTime, Long tenantId){
        List<ShopDayStatisticsDO> shopDayStatisticsDOList = shopDayStatisticsService.getDayStatisticsListByTime(startTime, endTime, tenantId);
        List<ShopDayUserDO> shopDayUserDOList = shopDayUserService.getDayUserListByTime(startTime, endTime, tenantId);
        ShopHandleStrategyDO strategyDO = shopHandleStrategyService.getValidStrategy(tenantId);
        List<ShopUserHolidayDO> holidayDOList = shopUserHolidayService.getUserHolidayListByTime(startTime, endTime, tenantId);

        ShopSalaryStatisticsRespVO respVO = new ShopSalaryStatisticsRespVO();
        respVO.setShopDetailList(shopDayStatisticsDOList);
        respVO.setUserDetailList(shopDayUserDOList);
        respVO.setStrategy(strategyDO);
        respVO.setHolidayList(holidayDOList);

        return respVO;
    }

    @Override
    public ShopStatisticsRespVO getShopDetail(LocalDateTime startTime, LocalDateTime endTime, Long tenantId){
        List<ShopDayStatisticsDO> shopDayStatisticsDOList = shopDayStatisticsService.getDayStatisticsListByTime(startTime, endTime, tenantId);
        List<ShopDayUserDO> shopDayUserDOList = shopDayUserService.getDayUserListByTime(startTime, endTime, tenantId);
        List<ShopUserHolidayDO> holidayDOList = shopUserHolidayService.getUserHolidayListByTime(startTime, endTime, tenantId);
        List<ShopMonthMemberDO> memberDetailList = shopMonthMemberService.getMonthMemberListByTime(startTime, endTime, tenantId);
        List<AdminUserRespDTO> userInfoDetailList = adminUserApi.getUserListByTenantId(tenantId);
        List<ShopAccountRecordDO> accountFixedList =shopAccountRecordService.getAccountRecordListFixed(tenantId);
        List<ShopAccountRecordDO> accountNoFixedList = shopAccountRecordService.getAccountRecordListByTimeNoFixed(startTime, endTime, tenantId);
        List<ShopStoreRecordDO> storeRecordDOList = shopStoreRecordService.getStroeRecordListByTime(startTime, endTime, tenantId, 1);

        Set<Long> userIds = CollectionUtils.convertSet(shopDayUserDOList, ShopDayUserDO::getUserId);
        List<ShopHandleStrategyUserRespVO> strategyList = shopHandleStrategyUserService.getHandleStrategeByUserIds(userIds);


        ShopStatisticsRespVO respVO = new ShopStatisticsRespVO();
        respVO.setShopDetailList(shopDayStatisticsDOList);
        respVO.setUserDetailList(shopDayUserDOList);
        respVO.setStrategyList(strategyList);
        respVO.setHolidayList(holidayDOList);
        respVO.setMemberDetailList(memberDetailList);
        respVO.setUserInfoDetailList(userInfoDetailList);
        respVO.setAccountFixedList(accountFixedList);
        respVO.setAccountNoFixedList(accountNoFixedList);
        respVO.setStoreRecordList(storeRecordDOList);

        return respVO;
    }

    @Override
    public ShopDayStatisticsRespVO getShopDayDetail(LocalDateTime dateTime, Long tenantId){
        ShopDayStatisticsRespVO respVO = new ShopDayStatisticsRespVO();
        String dateId = dateTime.format(DateTimeFormatter.ofPattern("yyyyMMdd"));
        ShopDayStatisticsDO dayStatisticsDO = shopDayStatisticsService.getDayStatisticsByDateId(dateId, tenantId);
        if(dayStatisticsDO == null){
            dayStatisticsDO = new ShopDayStatisticsDO();
            dayStatisticsDO.setTenantId(tenantId);

            dayStatisticsDO.setDateId(dateId); //日期id
            dayStatisticsDO.setTotalCount(0); //总支数
            dayStatisticsDO.setTotalPrice(0); //总流水
            dayStatisticsDO.setWalletPrice(0); //余额流水
            dayStatisticsDO.setCashPrice(0); //现金流水
            dayStatisticsDO.setTimesCharge(0); //次数支付

            dayStatisticsDO.setTotalMemberTimes(0); //总人次
            dayStatisticsDO.setVipTimes(0); //会员人次
            dayStatisticsDO.setNovipTimes(0); //非会员人次
            dayStatisticsDO.setNewMemberCount(0); //新人进店人次


            dayStatisticsDO.setChargeTotalCount(0); //充值人数
            dayStatisticsDO.setChargeTotalPrice(0); //充值总金额
            dayStatisticsDO.setChargeContinueCount(0); //续充人数
            dayStatisticsDO.setChargeNoContinueCount(0); //未续充人次
            dayStatisticsDO.setChargeFirstCount(0); //首充人次
            dayStatisticsDO.setChargeAfterTimesCount(0); //大于1次之后充值人次
        }

        respVO.setDayStatistics(dayStatisticsDO);

        DateTime time = new DateTime(dateTime);
        DateTime startTime = DateUtil.beginOfDay(time);
        DateTime endTime = DateUtil.endOfDay(time);

        Long orderCount = shopOrderMapper.selectCount(new LambdaQueryWrapper<ShopOrderDO>()
                .between(ShopOrderDO::getStartTime, startTime, endTime)
                .eq(ShopOrderDO::getTenantId, tenantId));
        respVO.setOrderCount(orderCount.intValue());

        return respVO;
    }

    @Override
    public void dayJob(){
        List<TenantDetailDO> tenantList = tenantDetailService.getValidTenantList();
        if(tenantList.isEmpty()){
            return;
        }

        List<Long> tenantIds = new ArrayList<>();
        for(int i = 0; i<tenantList.size(); i++){
            tenantIds.add(tenantList.get(i).getTenantId());
        }

        List<AdminUserRespDTO> userList = adminUserApi.getUserListByTenantIds(tenantIds);

        shopDayStatisticsService.dayJob(tenantIds);
        shopDayUserService.dayJob(userList);
    }
}
